﻿using cszmcaux;
using LKLCode.MotionControlCard.Zmotion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LKL_Code.MotionControlCard.Zmotion
{
   public class ZmcMotion:IMotion
    {
        private string ip;//本卡IP
        private IntPtr zmc_Handle;//控制卡句柄
        private StringBuilder ControllerId = new StringBuilder(), SoftType = new StringBuilder(), SoftVersion = new StringBuilder();

        /// <summary>
        /// ZMC轴列表
        /// </summary>
        public IAxis[] Axises { get { return _axises; } }
        public IAxis[] _axises = Array.Empty<IAxis>();
        //_axises被初始化为空数组，因此在InitializeAxes方法中，
        //可以安全地检查数组的长度，而无需担心_axises为null。

        /// <summary>
        /// ZMC插补对象列表
        /// </summary>
        public IInterpolation[] Interpolations { get { return _interpolations; } }
        public IInterpolation[] _interpolations = Array.Empty<IInterpolation>();



        /// <summary>
        /// 运动卡状态
        /// </summary>
        public MotionStatus MotionStatus
        {
            get
            {
                return motionStatus;
            }
        }
        private MotionStatus motionStatus;

        /// <summary>
        /// 连接控制卡
        /// </summary>
        /// <param name="ip"></param>
        public void ConnectEtherNet(string ip)
        {
            try
            {
                zmcaux.ZAux_OpenEth(ip, out zmc_Handle);
                if (zmc_Handle != (IntPtr)0)
                {
                    motionStatus.IsConnected = true;
                    this.ip = ip;
                }
                else
                {
                    motionStatus.IsConnected = false;
                    this.ip = string.Empty;
                    throw new Exception(string.Format("运动卡连接失败，尝试连接的IP:{0}", ip));
                }
            }

            catch (Exception e) { throw e; }

        }

        /// <summary>
        /// 断开控制器连接
        /// </summary>
        public void DisConnectEtherNet()
        {
            try
            {
                zmcaux.ZAux_Close(zmc_Handle);
                motionStatus.IsConnected = false;
                zmc_Handle = (IntPtr)0;//释放句柄
            }
            catch (Exception e) { throw e; }
        }

        public ZmcMotion()
        {
            motionStatus = new MotionStatus() { IsEmergentStop = false, IsPause = false, IsConnected = false };
        }

        /// <summary>
        /// 初始化所有轴
        /// </summary>
        /// <param name="axisesInitType"></param>
        /// <param name="axisTypes">轴类型列表，用于指定每个轴的类型</param>
        public void InitAllAixs(AxisInitType[] axisesInitType)
        {
            if (!motionStatus.IsConnected)
            {
                return;
            }
            _axises = new IAxis[axisesInitType.Length];
            for (int i = 0; i < axisesInitType.Length; i++)
            {
                IAxis axis = new ZmcAxis(zmc_Handle, axisesInitType[i]);
                _axises[i] = axis;
            }
        }

        /// <summary>
        /// 初始化所有插补
        /// </summary>
        public void InitAllInterpolation(List<int[]> listAxisIndex)
        {
            if (!motionStatus.IsConnected)
            {
                return;
            }
            _interpolations = new IInterpolation[listAxisIndex.Count];
            for (int i = 0; i < listAxisIndex.Count; i++)
            {
                IInterpolation intrepolation = new ZmcInterpolation(zmc_Handle, listAxisIndex[i]);
                _interpolations[i] = intrepolation;
            }
        }
        public void InitAllInterpolation(List<IAxis[]> listAxises)
        {
            List<int[]> listAxisInedx = new List<int[]>();
            for (int i = 0; i < listAxises.Count; i++)
            {
                int[] ints = new int[listAxises[i].Length];
                for (int j = 0; j < ints.Length; j++)
                {
                    ints[j] = listAxises[i][j].Index;
                }
                listAxisInedx.Add(ints);
            }
            InitAllInterpolation(listAxisInedx);
        }









        /// <summary>
        /// 急停
        /// </summary>
        public void EmergenStop()
        {
            motionStatus.IsEmergentStop = true;
            foreach (var axis in _axises)
            {
                axis.EmergentStop();
            }
        }

        /// <summary>
        /// 解除运动控制卡急停
        /// </summary>
        public void ResetEmergenStop()
        {
            motionStatus.IsEmergentStop = true;
            foreach (var axis in _axises)
            {
                axis.EmergentStop();
            }
        }

        /// <summary>
        /// 断开控制器连接
        /// </summary>
        public void Close()
        {
            if (!motionStatus.IsConnected)
            {
                return;
            }
            zmcaux.ZAux_Close(zmc_Handle);
        }

        /// <summary>
        /// 获取运动卡ID号
        /// </summary>
        /// <returns></returns>
        public string GetMotioCardId()
        {
            if (ControllerId.Length == 0)
            {
                GetMotioCardId();
            }
            return ControllerId.ToString();
        }

        private void GetControllerInfo()
        {
            if (!motionStatus.IsConnected)
            {
                throw new Exception("zmc Link fail");
            }
            zmcaux.ZAux_GetControllerInfo(zmc_Handle, SoftType, SoftVersion, ControllerId);
        }


        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            if (motionStatus.IsEmergentStop == true)
            {
                return;
            }
            motionStatus.IsPause = true;
            foreach (var axis in _axises)
            {
                axis.Pause();
            }
        }

        /// <summary>
        /// 重新开始
        /// </summary>
        public void Resume()
        {
            if (motionStatus.IsEmergentStop == true)
            {
                return;
            }
            foreach (var axis in _axises)
            {
                axis.Resume();
            }
            motionStatus.IsPause = false;
        }

    }
}
